import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import hilog from '@ohos.hilog';
import UIAbility from '@ohos.app.ability.UIAbility';
import Want from '@ohos.app.ability.Want';
import { UDPGetter } from '../pojo/UDPGetter'
import window from '@ohos.window';
import update from '@ohos.update';

export default class EntryAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
  }

  onDestroy(): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }

  onWindowStageCreate(windowStage: window.WindowStage): void {
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

    windowStage.loadContent('pages/Index', (err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
    });
    this.setlandscape();
    this.setSystemBar();
    //接收UDP数据，每次接收到后放到全局变量类中存储.
    this.openUDP();

  }

  onWindowStageDestroy(): void {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground(): void {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground(): void {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }


  //设置导航栏，状态栏不可见
  private async setSystemBar() {
    let windowClass = await window.getLastWindow(this.context)
    await windowClass.setWindowSystemBarEnable([])
  }

  //将应用设置为横屏
  private setlandscape() {
    //将应用设置为横屏
    let promise = window.getLastWindow(this.context);
    promise.then((data) => {
      //切换成横屏
      let orientation = window.Orientation.LANDSCAPE;
      data.setPreferredOrientation(orientation, (err) => {
      });
    })
  }

  private openUDP() {
    let uDPGetter = new UDPGetter()
    //开启UDP接收
    uDPGetter.udpInit()
    uDPGetter.udpSend()   //先要发一下开启状态，否则接收不到
  }


}


